package com.kitbox.hl.meshow.common.utils.pinyin;

/**
 *
 */

/**
 * <pre>
 * 此处对该文件进行描述.
 *
 *
 *
 * 修改记录:
 *
 *
 * </pre>
 *
 * @since 2008-5-4
 * @version 1.0
 * @author yuqiang
 *
 * <p>版权：银联数据<p>
 */
public class CnToSpell
{
	private static String getCnAscii(char cn) {
		byte[] bytes = (String.valueOf(cn)).getBytes();

		if (bytes == null || bytes.length > 2 || bytes.length <= 0) {
			return "";
		}
		if (bytes.length == 1) {
			return new String(bytes);
		}
		if (bytes.length == 2) {
			int hightByte = 256 + bytes[0];
			int lowByte = 256 + bytes[1];

			String ascii = hightByte + "-" + lowByte;

			return ascii;
		}

		return "";
	}


	private static String getSpellByAscii(String ascii) {
		if (ascii.indexOf("-") > -1)
		{
			return (String) CnAsciiLib.spellMap.get(ascii);
		} else {
			return ascii;
		}
	}

	/**
	 * 获取一个字符串的完整拼音
	 * @param cnStr
	 * @return
	 */
	public static String getFullSpell(String cnStr)
	{
		if (null == cnStr || "".equals(cnStr.trim())) {
			return cnStr;
		}

		char[] chars = cnStr.toCharArray();
		StringBuffer retuBuf = new StringBuffer();
		for (int i = 0, Len = chars.length; i < Len; i++) {
			String ascii = getCnAscii(chars[i]);
			if (ascii.length() == 0) {
				retuBuf.append(chars[i]);
			} else {
				String spell = getSpellByAscii(ascii);
				if (spell == null) {
				    char firstChar = chars[i];
				    if ((firstChar >= 48 && firstChar <= 57) || (firstChar >= 65 && firstChar <= 90) || (firstChar >= 97 && firstChar <= 122))
	                {
	                    retuBuf.append(firstChar);
	                }
				} else {
					retuBuf.append(spell);
				}
			}
		}

		return retuBuf.toString();
	}

	/**
	 * 把一个字符串中所有可转成拼音的字符 转化成拼音
	 * @param cnStr
	 * @return
	 */
	public static String getFirstSpell(String cnStr)
	{
		if (null == cnStr || "".equals(cnStr.trim())) {
			return cnStr;
		}
		char[] chars = cnStr.toCharArray();
		StringBuffer retuBuf = new StringBuffer();
		for (int i = 0, Len = chars.length; i < Len; i++) {
			String ascii = getCnAscii(chars[i]);
			char firstChar;
			if (ascii.length() == 1) {
				firstChar = chars[i];
			} else {

				String spell = getSpellByAscii(ascii);
				if (spell == null || "".equals(cnStr.trim())) {
				    firstChar = chars[i];
				} else {
				    firstChar = spell.charAt(0);
				}
			}
			if ((firstChar >= 48 && firstChar <= 57) || (firstChar >= 65 && firstChar <= 90) || (firstChar >= 97 && firstChar <= 122))
			{
			    retuBuf.append(firstChar);
			}
		}
		return retuBuf.toString();
	}

	/**
	 * 取第一个可以转换出来的拼音
	 * @param cnStr
	 * @return
	 */
	public static String getFirstWordSpell(String cnStr)
	{
		if (null == cnStr || "".equals(cnStr.trim())) {
			return cnStr;
		}
		char[] chars = cnStr.toCharArray();
		for (int i = 0, Len = chars.length; i < Len; i++) {
			String ascii = getCnAscii(chars[i]);
			if (ascii.length() == 1)
			{
				return ascii;
			} else
			{

				String spell = getSpellByAscii(ascii);

				if (spell == null || "".equals(spell.trim()))
				{
					continue;
				} else
				{
					return String.valueOf(spell.charAt(0));
				}
			}
		}
		return "";
	}
}
